package com.lft.juc03.lock8;

import java.util.concurrent.TimeUnit;
/**
 * 线程8锁——锁3.
 * 新增普通方法（playGame()）
 * 普通方法不需要抢锁，直接可以执行。同步方法锁的是this，调用者，同一个调用者是同一把锁。
 */
public class Lock3 {
	public static void main(String[] args) {
		Phone3 phone = new Phone3();
		new Thread(() -> {phone.senSms();}, "A").start();
		try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {e.printStackTrace();}
		new Thread(() -> {phone.call();}, "B").start();
		new Thread(() -> {phone.playGame();}, "C").start();
	}
}

class Phone3 {
	public synchronized void senSms() {
		// 新增Thread.sleep()给 senSms()
		try {TimeUnit.SECONDS.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}
		System.out.println(Thread.currentThread().getName() + " 发短信");
	}
	
	public synchronized void call() {
		System.out.println(Thread.currentThread().getName() + " 打电话");
	}
	
	public void playGame() {
		System.out.println(Thread.currentThread().getName() + " 玩游戏");
	}
}